/**
 *
 */
package com.naiterui.ehp.bs.doctor.remote;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;

import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.bo.doctor.product.CategoryBO;
import com.naiterui.ehp.bp.bo.doctor.product.CueWordBO;
import com.naiterui.ehp.bp.bo.doctor.product.ProductDetailBO;
import com.naiterui.ehp.bp.bo.doctor.product.ProductSearchParamBO;
import com.naiterui.ehp.bp.bo.emr.MedicalRecordScreenBO;
import com.naiterui.ehp.bp.bo.medication.MedicationDefaultUsageBO;
import com.naiterui.ehp.bp.constants.PaQrCodeEnum;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.utils.JsonMapper;
import com.naiterui.ehp.bp.utils.lang.StringUtil;
import com.naiterui.ehp.bp.utils.net.RestHttpUtil;
import com.naiterui.ehp.bs.doctor.vo.product.ProductInfoVO;

import lombok.AllArgsConstructor;

/**
 * @author guoyongxiang
 * @since 2.12.0
 */
@Service
@AllArgsConstructor
public class AdRemoter implements IAdRemoter {

    private final AdRemoterConfig adRemoterConfig;

    @Override
    public Integer getRequiredFlag() throws BusinessException {
        return RestHttpUtil.post()
                           .url(this.adRemoterConfig.getEmrRecordRequired())
                           .executeData(new ParameterizedTypeReference<ResponseVO<Integer>>() {
                           });
    }

    @Override
    public List<MedicalRecordScreenBO> getScreenList(Long doctorId, Long patientId) throws BusinessException {
        Map<String, String> params = new HashMap<>();
        params.put("doctorId", doctorId + "");
        params.put("patientId", patientId + "");
        return RestHttpUtil.post()
                           .url(this.adRemoterConfig.getScreenQueryUrl())
                           .paramMap(params)
                           .executeData(new ParameterizedTypeReference<ResponseVO<List<MedicalRecordScreenBO>>>() {
                           });
    }

    @Override
    public void remindPatinetSend(Long patientId, Long doctorId) throws BusinessException {
        Map<String, String> params = new HashMap<>();
        params.put("patientId", patientId + "");
        params.put("doctorId", doctorId + "");
        String url = this.adRemoterConfig.getRemindPatientSendUrl();
        RestHttpUtil.post().url(url).paramMap(params).executeData();
    }

    @Override
    public Integer judgeOneself(Long patientId, String name, Integer gender, Integer age, String ageUnit) throws BusinessException {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("patientId", String.valueOf(patientId));
        paramMap.put("name", name);
        paramMap.put("gender", gender + "");
        paramMap.put("age", age + "");
        paramMap.put("ageUnit", ageUnit);
        return RestHttpUtil.post()
                           .url(this.adRemoterConfig.getJudgeOneselfUrl())
                           .paramMap(paramMap)
                           .executeData(new ParameterizedTypeReference<ResponseVO<Integer>>() {
                           });
    }

    @Override
    public PageVO<ProductInfoVO> getProductInfo(ProductSearchParamBO searchParam) throws BusinessException {
        return RestHttpUtil.post()
                           .url(this.adRemoterConfig.getSearchProduct())
                           .body(JsonMapper.toJson(searchParam))
                           .executeData(new ParameterizedTypeReference<ResponseVO<PageVO<ProductInfoVO>>>() {
                           });
    }

    @Override
    public ProductDetailBO getProductDetailInfo(Long skuId) throws BusinessException {
        Map<String, Object> paramMap = new HashMap<>(6);
        paramMap.put("skuId", skuId);
        return RestHttpUtil.post()
                           .url(this.adRemoterConfig.getProductDetailUrl())
                           .paramMap(paramMap)
                           .executeData(new ParameterizedTypeReference<ResponseVO<ProductDetailBO>>() {
                           });
    }

    @Override
    public List<CueWordBO> getProductCueWord(String key) throws BusinessException {
        Map<String, String> paramMap = new HashMap<>(6);
        paramMap.put("key", key);
        return RestHttpUtil.post()
                           .url(this.adRemoterConfig.getCueWord())
                           .paramMap(paramMap)
                           .executeData(new ParameterizedTypeReference<ResponseVO<List<CueWordBO>>>() {
                           });
    }

    @Override
    public List<CategoryBO> getCategoryList() throws BusinessException {
        return RestHttpUtil.get()
                           .url(this.adRemoterConfig.getCategory())
                           .executeData(new ParameterizedTypeReference<ResponseVO<List<CategoryBO>>>() {
                           });
    }

    @Override
    public String getInviteCodeUrl(PaQrCodeEnum codeType, Long doctorId) throws BusinessException {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("codeType", codeType.getValue());
        paramMap.put("scene", doctorId.toString());
        return RestHttpUtil.get()
                           .url(this.adRemoterConfig.getInviteCodeUrl())
                           .paramMap(paramMap)
                           .executeData(new ParameterizedTypeReference<ResponseVO<String>>() {
                           });

    }

    @Override
    public Map<Long, MedicationDefaultUsageBO> getDefaultUsage(String skuIds) throws BusinessException {
        Set<Long> skuIdSet = StringUtil.convertLongStr2Set(skuIds, ",");
        return RestHttpUtil.post()
                           .url(this.adRemoterConfig.getMedicationDefaultUsageUrl())
                           .body(JsonMapper.toJson(skuIdSet))
                           .executeData(new ParameterizedTypeReference<ResponseVO<Map<Long, MedicationDefaultUsageBO>>>() {
                           });
    }

    @Override
    public void supervisePushSession(String sessionId) throws BusinessException, Exception {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("sessionId", sessionId);
        RestHttpUtil.get()
                    .paramMap(paramMap)
                    .url(this.adRemoterConfig.getSupervisePushSessionUrl())
                    .executeData();
    }
}
